<?php

if (isset($_GET['idRes']) )
{
	$idRes = $_GET['idRes'];
	if (isset($_GET['week']))
		$week = $_GET['week'];
		$weekATraiter = $week + 32;
		if ($weekATraiter > 52) $week -= 52;
	else{
		$week = date('W');
		$weekATraiter = $week;		
		$week -= 32;
		if ($week < 0) $week += 52;
	}
	if (isset($_GET['session'])) 
		$session = $_GET['session']; 
	$annee = ($weekATraiter<35 && date('W') > 35)? date('Y')+1 : date('Y');
}
if (isset($idRes) && isset($week))
{
	echo "BEGIN:VCALENDAR\n";
	echo "VERSION:2.0\n";
	echo "PRODID:-//Bast73 Corporaction//NONSGML v1.0//FR\n\n";
	function cmpTabEvent($a, $b)
	{
		return ($a->attributes->item(6)->value < $b->attributes->item(6)->value) ? -1 : 1;
	}
		
	$erreurRecup = false;

	$url = 'http://ade52-savoie.grenet.fr/ade/webapi?function=connect&login=ETUDIANT&password=etsa';
	$agent = '';
	$id = '';
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 2);   
	curl_setopt($ch, CURLOPT_COOKIE, $id);
	$ret = curl_exec($ch);
	$erreurRecup = curl_errno($ch);
	if (!$erreurRecup &&(strpos($ret, '<session') === false))
		$erreurRecup = true;
	
	if (!$erreurRecup)
	{
		curl_close($ch);
		$page = new DOMDocument();
		$page->loadXML($ret);
		
		$session = $page->getElementsByTagName("session")->item(0)->attributes->item(0)->value;
		$time = time();
		
		$url = "http://ade52-savoie.grenet.fr/ade/webapi?sessionId=$session&function=setProject&projectId=4";
	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $agent);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 4);   
		curl_setopt($ch, CURLOPT_COOKIE, $id);
		$ret = curl_exec($ch);
		if (curl_errno($ch))
		{
			$session = -1;
			$time = -1;
		}
		else
			curl_close($ch);
	}

	if (!$erreurRecup)
	{
		for ($weekNumber = 0; $weekNumber < 2; $weekNumber++){
			$weekATraiterFinal = $weekATraiter + $weekNumber;
			for ($jour = 0; $jour < 5; $jour ++){

				$weekBis = $week + 32 + $weekNumber;
				if ($weekBis > 52) $weekBis -= 52;
				if ($weekBis > date('W') || ($jour+1) >= (date('w'))){
					$weekToQuery = ($week + $weekNumber);
					//echo "test".$weekNumber;
					$url = "http://ade52-savoie.grenet.fr/ade/webapi?sessionId=$session&function=getEvents&resources=$idRes&detail=0&weeks=$weekToQuery&days=$jour"; //getEvents&resources=1165&detail=4&weeks=12&days=0
					$agent = '';
					$id = '';
					$ch = curl_init();
					curl_setopt($ch, CURLOPT_USERAGENT, $agent);
					curl_setopt($ch, CURLOPT_URL, $url);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($ch, CURLOPT_TIMEOUT, 4);   
					curl_setopt($ch, CURLOPT_COOKIE, $id);
			 
					$ret = curl_exec($ch);
					if (curl_errno($ch))
					{
						$erreurRecup = true;
					}
					else
						curl_close($ch);
				
					if (!$erreurRecup &&(strpos($ret, '<event') === false))
					{
						$erreurRecup = true;
					}
				}
			
				if ($erreurRecup && $idRes != ""  )
				{
					require("connection_mysql.inc.php");
					$requete = "SELECT xml from sauvegarde where id = $idRes AND semaine = $week AND jour = $jour";
					//echo "<!-- $requete -->";
					$resultRequete = mysql_query($requete) or die(mysql_query());
					$result = mysql_fetch_array($resultRequete);
					if ($result)
						$ret = $result['xml'];
				}
				if ($ret)
				{
					$page = new DOMDocument();
					$page->loadXML($ret);
					
				
					$heureActuelle = 2; //correspond a 7h30
					$events = $page->getElementsByTagName("event");
					$tabEvent = array();
					for ($j = 0; $j < $events->length; $j++)
					{
						$tabEvent[$j] = $events->item($j);
					}
					usort( $tabEvent , "cmpTabEvent" );
					
					
					for ($j = 0; $j < count($tabEvent); $j++)
					{
						$detailCours = $tabEvent[$j]->attributes->getNamedItem("name")->value ; // 4 - Nom du cours
						$heureDebut = $tabEvent[$j]->attributes->getNamedItem('slot')->value ; // 6 - Heure du cours
						$duree = $tabEvent[$j]->attributes->getNamedItem('duration')->value ; // 10 - DurÃ©e du cours
						$idActivity = $tabEvent[$j]->attributes->item(1)->value;
						$couleur = $tabEvent[$j]->attributes->getNamedItem('color')->value;
						
						$heureDebutTot = $heureDebut * 1/4 + 7.5;
						$heureDebutH = floor($heureDebutTot);
						$heureDebutM = ($heureDebutTot - $heureDebutH) * 60; 
						
						$heureFinTot = $heureDebutTot + $duree * 1/4;
						$heureFinH = floor($heureFinTot);
						$heureFinM = ($heureFinTot - $heureFinH) * 60;
						
						$ressourcesTemp = $tabEvent[$j]->childNodes->item(1);
						$ressources = $ressourcesTemp->childNodes;
						
						$prof = '';
						$salle = '';
						foreach ($ressources as $ressource)
						{
							if ($ressource->hasAttributes())
							{
								if ($ressource->attributes->item(4)->value == 'instructor')
									$prof = $ressource->attributes->item(5)->value;
								if ($ressource->attributes->item(4)->value == 'classroom')
									$salle = $ressource->attributes->item(5)->value;
							}
							
						}
						
						
						
						$dateAAfficher = trouver_date($weekATraiterFinal, $annee, $jour+1);
						$month = substr($dateAAfficher, 4, 2);
						$decalage = calculDecalage($annee, $month, $jour+1);
						$heureDebutH -= $decalage;
						$heureDebutH = ($heureDebutH<10)? "0".$heureDebutH : $heureDebutH;
						$heureDebutM = ($heureDebutM<10)? "0".$heureDebutM : $heureDebutM;
						$heureDebutAAfficher = $heureDebutH.$heureDebutM."00Z";
						$heureFinH -= $decalage;
						$heureFinH = ($heureFinH<10)? "0".$heureFinH : $heureFinH;
						$heureFinM = ($heureFinM<10)? "0".$heureFinM : $heureFinM;
						$heureFinAAfficher = $heureFinH.$heureFinM."00Z";				
						
						echo "BEGIN:VEVENT\n";
						echo "UID:".$dateAAfficher."T".$heureDebutAAfficher."-".$detailCours."\n";
						echo "DTSTART:".$dateAAfficher."T".$heureDebutAAfficher."\n";
						echo "DTEND:".$dateAAfficher."T".$heureFinAAfficher."\n";				
						echo "SUMMARY:$detailCours\n";		 	
						echo "LOCATION:$salle\n";
						echo "DESCRIPTION:professeur : $prof\n";
						echo "END:VEVENT\n\n";
					}
				}
			}
		}
	}
	echo "END:VCALENDAR\n";
}

function trouver_date($sem, $annee, $j)
{
	$jour_dans_le_mois = array();
	$mois = 1;
	$jour = 0;
	
	for($a=1;$a<=365+date('L');$a++)
	{
		for($i=1;$i<=12;$i++)
		{
			$jour += date('t', mktime(0, 0, 0, $i, 1, $annee));
			$jour_dans_le_mois[$i] = $jour;
		}
		 
		$today = mktime(0, 0, 0, $mois, $a, $annee);
		$semaine = date('W', $today);
		$day = date('N', $today);
		$date = date('Ymd', $today);
		if(in_array($a, $jour_dans_le_mois)) $mois++;
		 
		if($semaine == $sem && $day == $j)
			return $date;
	}
}

function calculDecalage($annee, $month, $jour){
	$today = mktime(0, 0, 0, $month, $jour, $annee);
	$decalage = date('O', $today);
	return substr($decalage, 1, 2);
}

?>


















